//
//  QcodeViewController.m
//  pocketNews
//
//  Created by DS on 2018/10/31.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "QcodeViewController.h"

#import "MMScanViewController.h"



@interface QcodeViewController ()

//二维码与条形码
Strong UIView * photoView;
Strong UIImageView * photoImageView;
Strong UILabel * photoLabel;
Strong UILabel * photoLabel1;
Strong UIView * photoLineView;


//生成二维码
Strong UIView * qrCodeView;
Strong UIImageView * qrcodeImageView;
Strong UILabel * qrcodeLabel;
Strong UILabel * qrcodeLabel1;

//生成条形码

Strong UIView * barView;
Strong UIView * barImageView;
Strong UILabel * barLabel;
Strong UILabel * barLabel1;
Strong UIView * barLineView;

//请输入二维码的文字内容
Strong UIView * greenView;
Strong UITextField * inputTextField;
Strong UIButton * nextBtn;


Strong UIView * lineView;

Strong UIImageView * picImageView;

//标记生成二维码还是条形码

Assign BOOL isMarked;

@end

@implementation QcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"扫码工具";
    
    UIButton *btn1 =[[UIButton alloc]initWithFrame:CGRectMake(0,0, 22, 22)];
    
    [btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setBackgroundImage:[[UIImage imageNamed:@"fanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    self.navigationItem.leftBarButtonItem  = item;
    
    [self.view addSubview:self.photoView];
    [self.view addSubview:self.qrCodeView];
    [self.view addSubview:self.barView];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.greenView];
    [self.view addSubview:self.picImageView];
    
    self.isMarked = NO;
}

-(void)onClick:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark ----

- (void)photoViewTapClick{
    
    
    MMScanViewController *scanVc = [[MMScanViewController alloc] initWithQrType:MMScanTypeAll onFinish:^(NSString *result, NSError *error) {
        if (error) {
            NSLog(@"error: %@",error);
        } else {
            NSLog(@"扫描结果：%@",result);
            [self showInfo:result];
        }
    }];
    [self.navigationController pushViewController:scanVc animated:YES];
    
    
}

- (void)qrCodeViewTapClick{
    
    self.isMarked = NO;
    
    self.greenView.backgroundColor = getColor(@"FA5AB1");
   
    
}

- (void)barViewTapClick{
    
   
    self.isMarked = YES;
    
    self.greenView.backgroundColor = getColor(navColor);
  
}


#pragma mark - Error handle
- (void)showInfo:(NSString*)str {
    [self showInfo:str andTitle:@"提示"];
}

- (void)showInfo:(NSString*)str andTitle:(NSString *)title
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:str preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction *action1 = ({
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:NULL];
        action;
    });
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:NULL];
}





#pragma Init----

- (UIView *)photoView{
    
    if (!_photoView) {
        
        _photoView = [UIView addViewWithFrame:CGRectMake(0, height64, SCREEN_WIDTH /3, SCREEN_WIDTH / 3 + UNIT_HEIGHT(50)) WithBackgroundColor:getColor(whiteColor)];
        
        _photoView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoViewTapClick)];
        [_photoView addGestureRecognizer:tap];
        
        
        _photoImageView = [UIImageView addImgWithImage:@"zixun"];
        
        _photoLabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"扫码" AndFont:15 AndAlpha:1.0 AndColor:getColor(@"111111")];
        
        _photoLabel1 = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"二维码与条形码" AndFont:15 AndAlpha:1.0 AndColor:getColor(@"111111")];
        
        _photoLineView = [UIView addLineViewColor:getColor(dividerColor)];
        
        [_photoView sd_addSubviews:@[self.photoImageView,self.photoLabel,self.photoLabel1,self.photoLineView]];
        
        [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(UNIT_HEIGHT(30));
            make.centerX.mas_equalTo(self.photoView.mas_centerX);
            make.height.mas_equalTo(40);
            make.width.mas_equalTo(40);
        }];
        
        [self.photoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.photoImageView.mas_bottom).offset(UNIT_HEIGHT(10));
            make.centerX.mas_equalTo(self.photoView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
            
        }];
        
        [self.photoLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.photoLabel.mas_bottom).offset(UNIT_HEIGHT(10));
            make.centerX.mas_equalTo(self.photoView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.photoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.width.mas_equalTo(1);
        }];
        
    }
    
    return _photoView;
}

- (UIView *)qrCodeView{
    
    if (!_qrCodeView) {
        
        _qrCodeView = [UIView addViewWithFrame:CGRectMake(SCREEN_WIDTH / 3, height64, SCREEN_WIDTH / 3, SCREEN_WIDTH / 3 + + UNIT_HEIGHT(50)) WithBackgroundColor:getColor(whiteColor)];
        
        _qrCodeView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(qrCodeViewTapClick)];
        [_qrCodeView addGestureRecognizer:tap];
        
        _qrcodeImageView = [UIImageView addImgWithImage:@"chengrenyongpin"];
        
        _qrcodeLabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"生成" AndFont:15 AndAlpha:1.0 AndColor:getColor(@"111111")];
         _qrcodeLabel1 = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"二维码" AndFont:15 AndAlpha:1.0 AndColor:getColor(@"111111")];
        
        [self.qrCodeView sd_addSubviews:@[self.qrcodeImageView,self.qrcodeLabel,self.qrcodeLabel1]];
        
        [self.qrcodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(30));
            make.centerX.mas_equalTo(self.qrCodeView.mas_centerX);
            make.height.mas_equalTo(40);
            make.width.mas_equalTo(40);
        }];
        
        [self.qrcodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.qrcodeImageView.mas_bottom).offset(UNIT_HEIGHT(10));
            make.centerX.mas_equalTo(self.qrCodeView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
            
        }];
        
        [self.qrcodeLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.qrcodeLabel.mas_bottom).offset(UNIT_HEIGHT(10));
            make.centerX.mas_equalTo(self.qrCodeView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
            
        }];
        
    }
    
    return _qrCodeView;
}

- (UIView *)barView{
    
    if (!_barView) {
        _barView = [UIView addViewWithFrame:CGRectMake(SCREEN_WIDTH / 3 * 2, height64, SCREEN_WIDTH / 3, SCREEN_WIDTH / 3 + UNIT_HEIGHT(50)) WithBackgroundColor:getColor(whiteColor)];
        
        _barView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(barViewTapClick)];
        [_barView addGestureRecognizer:tap];
        
        _barImageView = [UIImageView addImgWithImage:@"zuji"];
        
        _barLabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"生成" AndFont:15 AndAlpha:1.0 AndColor:getColor(@"111111")];
        
        _barLabel1 = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"条形码" AndFont:15 AndAlpha:1.0 AndColor:getColor(@"111111")];
        
        _barLineView = [UIView addLineViewColor:getColor(dividerColor)];
        
        [self.barView sd_addSubviews:@[self.barImageView,self.barLabel,self.barLabel1,self.barLineView]];
     
        [self.barImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(UNIT_HEIGHT(30));
            make.centerX.mas_equalTo(self.barView.mas_centerX);
            make.height.mas_equalTo(40);
            make.width.mas_equalTo(40);
            
        }];
        
        [self.barLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.barImageView.mas_bottom).offset(UNIT_HEIGHT(10));
            make.centerX.mas_equalTo(self.barView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
            
        }];
        
        [self.barLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.barLabel.mas_bottom).offset(UNIT_HEIGHT(10));
            make.centerX.mas_equalTo(self.barView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.barLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.width.mas_equalTo(1);
        }];
        
        
    }
    
    return _barView;
    
}

- (UIView *)lineView{
    
    if (!_lineView) {
        
        _lineView = [UIView addViewWithFrame:CGRectMake(0, height64 + SCREEN_WIDTH / 3 + UNIT_HEIGHT(50), SCREEN_WIDTH, 1) WithBackgroundColor:getColor(dividerColor)];
    }
    
    return _lineView;
    
}

- (UIView *)greenView{
    
    if (!_greenView) {
        
        _greenView = [UIView addViewWithFrame:CGRectMake( 20, height64 + SCREEN_WIDTH / 3 + UNIT_HEIGHT(50) + UNIT_HEIGHT(20), SCREEN_WIDTH - 40, UNIT_HEIGHT(100)) WithBackgroundColor:getColor(@"FA5AB1")];
        _greenView.layer.cornerRadius = 5;
        _greenView.layer.masksToBounds = YES;
        
        _inputTextField = [UITextField addTextFieldWithPlaceholderStr:@"请输入文字内容" AndFont:20];
        
        
        _nextBtn = [UIButton addBtnTitle:@"生成" AndTitleColor:getColor(whiteColor) AndTitleFont:20 WithTarget:self action:@selector(nextBtnClick)];
       
        [_greenView sd_addSubviews:@[self.inputTextField,self.nextBtn]];
        
        [self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(10));
            make.height.mas_equalTo(UNIT_HEIGHT(30));
            make.centerX.mas_equalTo(self.greenView.mas_centerX);
            make.width.mas_equalTo(UNIT_WIDTH(200));
        }];
        
        [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.inputTextField.mas_bottom).offset(UNIT_HEIGHT(10));
            make.centerX.mas_equalTo(self.greenView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(50));
            make.width.mas_equalTo(UNIT_WIDTH(50));
        }];
    }
    
    return _greenView;
    
}

- (UIImageView *)picImageView{
    
    if (!_picImageView) {
        
        _picImageView = [UIImageView addImgWithFrame:CGRectMake( 80, height64 + SCREEN_WIDTH / 3  + UNIT_HEIGHT(50) + UNIT_HEIGHT(100) + UNIT_HEIGHT(40), SCREEN_WIDTH - 160, SCREEN_WIDTH - 160) AndImage:@""];
        
        _picImageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapClick)];
        
        [_picImageView addGestureRecognizer:tap];
        
    }
    
    return _picImageView;
    
}

- (void)nextBtnClick{
    
    if (self.isMarked) {
        
        if (_inputTextField.text == nil || _inputTextField.text.length == 0) {
            _inputTextField.text = @"1234567890";
        }
        
        
        UIImage *image = [MMScanViewController createBarCodeImageWithString:_inputTextField.text QRSize:CGSizeMake(250, 150) QRColor:[UIColor blackColor] bkColor:[UIColor colorWithRed:0.318 green:0.690 blue:0.839 alpha:1.00]];
        //如果不需要设置背景色以及前景色，则使用下面代码  默认白色底黑色码
        //UIImage *image = [ScanViewController createBarCodeImageWithString:_linkTfd.text barSize:CGSizeMake(250, 150)];
        
        [_picImageView setImage: image];
        
        
    }else{
    
        if (_inputTextField.text == nil || _inputTextField.text.length == 0) {
            _inputTextField.text = @"http://www.baidu.com";
        }
        
        UIImage *image = [MMScanViewController createQRImageWithString:_inputTextField.text QRSize:CGSizeMake(250, 250) QRColor:[UIColor blackColor] bkColor:[UIColor colorWithRed:0.318 green:0.690 blue:0.839 alpha:1.00]];
        //如果不需要设置背景色以及前景色，则使用下面代码  默认白色底黑色码
        //UIImage *image = [ScanViewController createQRImageWithString:_linkTfd.text QRSize:CGSizeMake(250, 250)];
        [_picImageView setImage: image];
    }
  
}

- (void)imageTapClick{
    
    if(_picImageView.image) {
        UIImageWriteToSavedPhotosAlbum(_picImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
    } else {
        [self showInfo:@"请先生成二维码或者条形码"];
    }
    
    
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    if(error) {
        [self showInfo:[NSString stringWithFormat:@"error: %@",error]];
    } else {
        [self showInfo:@"保存成功"];
    }
}




@end
